'use strict'

let util = require('util');
let fs = require('fs');
let models = require('../modules/models');
let Fiber = require('fibers');

let studentService = require('../modules/services/student');
let checkTimeSetService = require('../modules/services/checktimeset');
let userService = require('../modules/services/user');

let School = models.School;
let Classroom = models.Classroom;
let Grade = models.Grade;
let NatureClass = models.NatureClass;
let Student = models.Student;
let FestivalTip = models.FestivalTip;
let Festival = models.Festival;
let Teacher = models.Teacher;
let Attendance = models.Attendance;
let AttendanceValue = models.AttendanceValue;
let AttendanceStatus = models.AttendanceStatus;
let Track = models.Track;
let TrackType = models.TrackType;

let SchoolDao = models.SchoolDao;
let ClassroomDao = models.ClassroomDao;
let GradeDao = models.GradeDao;
let NatureClassDao = models.NatureClassDao;
let StudentDao = models.StudentDao;
let UserDao = models.UserDao;
let CardDao = models.CardDao;
let CheckTimeSetDao = models.CheckTimeSetDao;
let CheckTimeDao = models.CheckTimeDao;
let FestivalTipDao = models.FestivalTipDao;
let FestivalDao = models.FestivalDao;
let TeacherDao = models.TeacherDao;
let AttendanceDao = models.AttendanceDao;
let AttendanceValueDao = models.AttendanceValueDao;
let TrackDao = models.TrackDao;

function createStudents(studentData) {
    Object.keys(studentData).forEach(schoolName => {
        let classList = Object.keys(studentData[schoolName]);
        classList.forEach(className => {
            let csvFilePath = util.format('utils/data/%s/%s/list.csv', schoolName, className);
            let csvFileContent = fs.readFileSync(csvFilePath).toString();
            let lines = csvFileContent.split('\n');
            lines.pop();
            lines.shift();

            lines.forEach(line => {
                let fields = line.split(',');

                let sid = fields[0].trim();
                let name = fields[1].trim();

                let student = StudentDao.findOne({
                    name: name
                });

                if ( student ) {
                    return;
                }

                let iflyId = studentData[schoolName][className].iflyId;
                console.log(className);
                console.log(sid);
                console.log(name);
                console.log(iflyId);
                student = studentService.createStudent(sid, name);
                StudentDao.create(student);
                NatureClassDao.update({
                    iflyId: iflyId
                }, {
                    $push: {
                        students: student.id
                    }
                });
            });
        });
    });

    return studentData;
}

let StudentData = {
    '合肥168中学': {
        '高一（1）班': {
            iflyId: "2134000017000329417"
        },
        '高一（27）班': {
            iflyId: "2134000017000329418"
        }
    },
    '合肥市第四十二中学/': { 
        '七（1）班': {
            iflyId: "2134000017000288092"
        }, 
        '七（12）班': {
            iflyId: "2134000017000286616"
        } 
    },
    '合肥市绿怡小学': { 
        '二（2）班': {
            iflyId: "2300000017000013089"
        }, 
        '三（5）班': {
            iflyId:  "2300000017000013087"
        } 
    }
};

Fiber(function() {
    let studentData = createStudents(StudentData);
    
    process.exit();
}).run();

